package com.huaiwei.usmManager.modules.usm.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaiwei.usmManager.common.utils.Constant;
import com.huaiwei.usmManager.common.utils.NumberUtils;
import com.huaiwei.usmManager.common.utils.PageUtils;
import com.huaiwei.usmManager.common.utils.Query;
import com.huaiwei.usmManager.modules.usm.dao.ReportedLossDao;
import com.huaiwei.usmManager.modules.usm.dao.TypeDao;
import com.huaiwei.usmManager.modules.usm.entity.GoodsEntity;
import com.huaiwei.usmManager.modules.usm.entity.ReportedLossEntity;
import com.huaiwei.usmManager.modules.usm.entity.ResourceEntity;
import com.huaiwei.usmManager.modules.usm.entity.TypeEntity;
import com.huaiwei.usmManager.modules.usm.service.ReportedLossService;
import com.huaiwei.usmManager.modules.usm.service.ResourceService;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ReportedLossServiceImpl extends
    ServiceImpl<ReportedLossDao, ReportedLossEntity> implements ReportedLossService {

  @Autowired
  private ResourceService resourceService;

  /**
   * 分页查询和筛选
   * @param params
   * @return
   */
  @Override
  public PageUtils queryPage(Map<String, Object> params) {
    Integer status = NumberUtils.obj2Integer(params.get("status"));
    String date = (String) params.get("date");

    String beginDate = "";
    String endDate = "";
    if (StringUtils.isNotEmpty(date)) {
      beginDate = date.split(",")[0];
      endDate = date.split(",")[1];
    }

    IPage<ReportedLossEntity> page = this.page(new Query<ReportedLossEntity>().getPage(params),
        new QueryWrapper<ReportedLossEntity>()
            .eq(status != null, "status", status)
            .between(StringUtils.isNotEmpty(date), "date", beginDate, endDate)
            .orderByDesc("loss_id")
    );

    return new PageUtils(page);
  }

  /**
   * 根据lossId查询报损详情信息
   * @param lossId
   * @return
   */
  @Override
  public ReportedLossEntity getInfoById(Integer lossId) {
    /*查询报损的信息*/
    ReportedLossEntity reportedLoss = this.getById(lossId);
    /*查询报损图片列表*/
    List<ResourceEntity> imageList = getImageListByLossId(lossId);
    reportedLoss.setImageList(imageList);
    return reportedLoss;
  }

  /**
   * 添加和更新报损信息
   * @param reportedLoss
   */
  @Override
  public void insertOrUpdate(ReportedLossEntity reportedLoss) {
    if (reportedLoss.getLossId() != null) {
      /*删除关联信息*/
      deleteRelationInfo(reportedLoss.getLossId());
    }
    if (this.saveOrUpdate(reportedLoss)) {
      /*插入图片信息*/
      if (reportedLoss.getImageNames() != null && reportedLoss.getImageNames().length > 0) {
        List<ResourceEntity> imageList = new ArrayList<>();
        for (String image : reportedLoss.getImageNames()) {
          ResourceEntity resourceEntity = new ResourceEntity();
          resourceEntity.setResourceId(reportedLoss.getLossId());
          resourceEntity.setType(Constant.LOSS);
          resourceEntity.setImageUrl(image);
          resourceEntity.setStatus(Constant.ON);
          imageList.add(resourceEntity);
        }
        resourceService.saveBatch(imageList);
      }
    }
  }

  /**
   * 删除报损
   * @param lossId
   */
  @Override
  public void deleteInfo(Integer lossId) {
    if (this.removeById(lossId)){
      deleteRelationInfo(lossId);
    }
  }

  @Override
  public List<ResourceEntity> getImageListByLossId(Integer lossId) {
    return resourceService.list(
        new QueryWrapper<ResourceEntity>()
            .eq("type", Constant.LOSS)
            .eq("resource_id", lossId)
    );
  }

  //删除关联信息
  private void deleteRelationInfo(Integer lossId) {
    /*删除图片*/
    resourceService.remove(new QueryWrapper<ResourceEntity>()
        .eq("type", Constant.LOSS)
        .eq("resource_id", lossId)
    );
  }

}
